package com.business.system.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.business.base.action.BaseAction;
import com.business.base.tools.JsonUtil;
import com.business.system.pojo.TabCarParts;
import com.business.system.service.ITabCarPartsService;
import com.utility.Page;

@Controller
@ParentPackage("struts-default")
@Namespace("/")
public class CarPartsAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private TabCarParts carParts;
	@Autowired
	private ITabCarPartsService carPartsService;
	private int id;

	@Action(value = "CarPartsAction_list")
	public String list() throws Exception {
		this.pageNo = (int) Math.floor((start + limit) / limit);
		if (paramsCode != null && !paramsCode.equals("")) {
			carParts = (TabCarParts) JsonUtil.getObject4JsonString(paramsCode,
					TabCarParts.class);
		} else {
			carParts = new TabCarParts();
		}

		Page<TabCarParts> page = this.carPartsService.findListPage(
				this.carParts, this.pageNo, this.limit);
		List<TabCarParts> list = page.getResults();
		JSONArray array = JSONArray.fromObject(list);
		String result = "{totalCount:" + page.getTotalRecord() + ",root:"
				+ array.toString() + "}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "CarPartsAction_all")
	public String all() throws Exception {
		carParts = new TabCarParts();
		List<TabCarParts> list = this.carPartsService.findList(carParts);
		JSONArray array = JSONArray.fromObject(list);
		String result = "{totalCount:" + list.size() + ",root:"
				+ array.toString() + "}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "CarPartsAction_add")
	public String add() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			carParts = (TabCarParts) JsonUtil.getObject4JsonString(paramsCode,
					TabCarParts.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.carPartsService.insert(carParts);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "CarPartsAction_edit")
	public String edit() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			carParts = (TabCarParts) JsonUtil.getObject4JsonString(paramsCode,
					TabCarParts.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.carPartsService.update(this.carParts);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "CarPartsAction_delete")
	public String delete() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		try {
			this.carPartsService.delete(id);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public TabCarParts getCarParts() {
		return carParts;
	}

	public void setCarParts(TabCarParts carParts) {
		this.carParts = carParts;
	}

}
